package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    //添加购物车
    @Override
    public R saveWith(ShoppingCart shoppingCart) {
        //获取用户id,设置用户id
        Long userId = UserHolder.getUserId();
        shoppingCart.setUserId(userId);
        /* 菜品或者套餐是否点过
          如何知道烧鹅被点过了？？ 根据烧鹅的菜品id，查询下数据库中有没有烧鹅这个数据
          如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
          如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
         */
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        // 判断当前用户点的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();

        //说明点的是菜品
            qw.eq(dishId!=null, ShoppingCart::getDishId, dishId);
       //如果不是菜品，那么一定是套餐
            Long setmealId = shoppingCart.getSetmealId();
            qw.eq(setmealId !=null, ShoppingCart::getSetmealId, setmealId);


        ShoppingCart cart = shoppingCartMapper.selectOne(qw);

        // 第一次点的时候是新增 ，后面在点击的时候是修改
        if (cart == null){
            // 如果没有，则是新增
            // 设置 number=1
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCartMapper.insert(shoppingCart);
            cart = shoppingCart;

        }else {
            // 如果有，则是更新
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateById(cart);

        }
        return R.success(cart);
    }

    //取消购物车
    @Override
    public R subShoppingCart(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, UserHolder.getUserId());

        qw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        qw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        ShoppingCart cart = shoppingCartMapper.selectOne(qw);

        // number -1 然后操作：
        Integer number = cart.getNumber();
        number--;
        cart.setNumber(number);

        if (number > 0){
            // >0  update更新 update set number=? where id = ?
            shoppingCartMapper.updateById(cart);
        }else {
            // <= 0  如何操作？
            // delete from shopping_cart where dish_id=? and user_id = ?
            shoppingCartMapper.delete(qw);
        }

        return R.success(cart);
    }
}
